<?php


namespace app\controller;

/**
 * 小区
 * @package app\controller
 */
class Aizone extends BackendController{
    protected $model = null;
    protected $noNeedLogin=['*'];
    public function _initialize()
    {
        //parent::_initialize();
        $this->model = new \app\model\Aizone();

    }

    public function after_index($lists) {
        if($lists){
            $ids=[];
            foreach ($lists as $list) {
                $ids[]=$list['name_code'];
            }
            $counts=\app\model\MqttUserManage::where('address_id','in',$ids)->group('address_id')->column('count(*) count','address_id');
            foreach ($lists as &$list) {
                if(isset($counts[$list['name_code']])){
                    $list['counts']=$counts[$list['name_code']];
                }
            }
        }
        return $lists;
    }

    public function ana_where($where){
        foreach ($where as &$item){
            if($item[0]=='name'){
                //$item=[['name',$item[1],'%'.$item[2].'%'],['name_code',$item[1],'%'.$item[2].'%'],'OR'];
                //$where[]=['name_code','LIKE','%'.$item[2].'%'];
                //$where['_logic']='or';
                $item=['name|name_code',$item[1],'%'.$item[2].'%'];
            }
        }
        return $where;
    }

    public function after_add($params){
        \app\model\ViidDeviceType::create([
            'pid'=>'1',
            'name'=>$params['name'],
            'count'=>0,
            'aizone_id'=>$params['id']
        ]);
    }

    public function before_del($lists) {
        $ids=[];
        foreach ($lists as $list) {
            $ids[]=$list['id'];
        }
        \app\model\ViidDeviceType::where('aizone_id','in',$ids)->delete();

    }

    public function after_edit($params,$old) {
        \app\model\ViidDeviceType::where('aizone_id','=',$params['id'])->save(['name'=>$params['name']]);
    }

}